﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.Module
{
    public partial class ucPromotionProducts : System.Web.UI.UserControl
    {
        cmsProductBL data = new cmsProductBL();
        cmsProductDO itemProductDO = new cmsProductDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["shop"] == null)
                {
                    Session["shop"] = new MyShopCart();
                }
                rptNewProductDataSource();
            }
        }

        private void rptNewProductDataSource()
        {
            rptpromotionProduct.DataSource = new cmsProductBL().SelectFrontPage(4, -1, -1, 1, -1);
            rptpromotionProduct.DataBind();
        }
        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        protected void rptPromotionProduct_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int productID = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName.Equals("AddToCart"))
            {
                AddItem(productID);
            }
        }
        private void AddItem(int productID)
        {
            cmsProductDO itemDO = new cmsProductDO();
            itemDO.ProductID = productID;
            //itemDO.ProductID = idProduct;
            cmsProductDO product = data.Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                string LH="Liên hệ";
                if (product.IsPromotion) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    if (product.SalePrice == 0 || product.SalePrice.ToString().Equals(""))
                        if (product.Price != 0)
                            item.Cost = (decimal)product.Price;
                        else
                            //item.Cost = ;
                    {
                        item.Cost = (decimal)product.SalePrice;
                    }
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    if (product.Price == 0 || product.Price.ToString().Equals(""))
                        item.Cost = 0;
                        
                    else
                    {

                        item.Cost = (decimal)product.Price;
                    }
                }
                item.Quantity = 1;
                ((MyShopCart)Session["shop"]).Add(item);
            }
            //Response.Redirect(this.Request.Url.ToString());
            Ultility.Alert("Đặt hàng thành công", Request.Url.AbsolutePath);
            // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "<script type='text/javascript'>$(document).ready(fuction(){ $('[id*=btnPopup]').live('click', function () { $('#dialog').dialog({title: 'jQuery Dialog Popup',buttons: { Close: function () {$(this).dialog('close'); }} }); return false;}); });</script>", false);
        }
    }
}